package com.jdrx.ocp.api;

import com.jdrx.logger.bizlogs.anno.BizLogger;
import com.jdrx.ocp.beans.dto.setting.CreateSettingDTO;
import com.jdrx.ocp.beans.dto.setting.QuerySettingDTO;
import com.jdrx.ocp.beans.dto.setting.UpdateSettingBatchDTO;
import com.jdrx.ocp.beans.dto.setting.UpdateSettingDTO;
import com.jdrx.ocp.beans.entity.SettingPO;
import com.jdrx.ocp.service.SettingService;
import com.jdrx.ocp.utils.Constants;
import com.jdrx.platform.commons.rest.beans.enums.EApiStatus;
import com.jdrx.platform.commons.rest.beans.vo.ResposeVO;
import com.jdrx.platform.commons.rest.exception.BizException;
import com.jdrx.platform.commons.rest.factory.ResponseFactory;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

import static com.jdrx.platform.common.support.gateway.GwConstants.TRANSPARENT_USERID_FEILD;

/**
 * @Author: Huangxg
 * @Date: 2018/11/20 15:21
 */
@RestController
@RequestMapping("/api/0/setting/")
@Api(value = "系统配置管理", description = "系统配置管理")
public class SettingApi {

    @Autowired
    SettingService settingService;

    @RequestMapping(value = "queryByPage", method = RequestMethod.POST)
    @ApiOperation(value = "配置查询", notes = "配置查询")
    public ResposeVO queryByPage(@ApiParam(required = true)
                                 @RequestBody @Valid QuerySettingDTO querySettingDTO) throws BizException {
        return ResponseFactory.ok(settingService.listByPage(querySettingDTO));
    }

    @RequestMapping(value = "queryAll", method = RequestMethod.POST)
    @ApiOperation(value = "配置查询", notes = "配置查询")
    public ResposeVO queryAll() throws BizException {
        return ResponseFactory.ok(settingService.queryAllForGroup());
    }

    @RequestMapping(value = "queryByKey", method = RequestMethod.POST)
    @ApiOperation(value = "获取一个配置信息", notes = "获取一个配置信息")
    public ResposeVO queryByKey(@ApiParam(required = true)
                                @RequestBody @Valid String key) throws BizException {
        return ResponseFactory.ok(settingService.getByKey(key));
    }

    @RequestMapping(value = "create", method = RequestMethod.POST)
    @ApiOperation(value = "添加配置", notes = "添加配置")
    //@BizLogger(description = "添加配置", serviceId = Constants.SERVICE_ID)
    public ResposeVO create(@RequestBody @Valid CreateSettingDTO createSettingDTO,
                            @RequestHeader(TRANSPARENT_USERID_FEILD) Integer uId,
                            @RequestHeader(value = Constants.TENANT_ID, required = false, defaultValue = "") String tid) throws BizException {
        SettingPO settingPO = new SettingPO();
        BeanUtils.copyProperties(createSettingDTO, settingPO);
        if (settingService.addSetting(settingPO, tid) > 0) {
            settingService.keyMap(tid);
            return ResponseFactory.ok("操作成功");
        } else {
            return ResponseFactory.err("添加失败", EApiStatus.ERR_SYS);
        }
    }

    @RequestMapping(value = "update", method = RequestMethod.POST)
    @ApiOperation(value = "更新配置", notes = "更新配置")
    //@BizLogger(description = "更新配置", serviceId = Constants.SERVICE_ID)
    public ResposeVO update(@RequestBody @Valid UpdateSettingDTO updateSettingDTO,
                            @RequestHeader(TRANSPARENT_USERID_FEILD) Integer uId,
                            @RequestHeader(value = Constants.TENANT_ID, required = false, defaultValue = "") String tid) throws BizException {
        SettingPO settingPO = new SettingPO();
        BeanUtils.copyProperties(updateSettingDTO, settingPO);
        if (settingService.updateSetting(settingPO, tid) >= 0) {
            settingService.keyMap(tid);
            return ResponseFactory.ok("操作成功");
        } else {
            return ResponseFactory.err("更新失败", EApiStatus.ERR_SYS);
        }
    }

    @RequestMapping(value = "delete", method = RequestMethod.POST)
    @ApiOperation(value = "删除配置", notes = "删除配置")
    //@BizLogger(description = "删除配置", serviceId = Constants.SERVICE_ID)
    public ResposeVO delete(@RequestBody @Valid String key,
                            @RequestHeader(TRANSPARENT_USERID_FEILD) Integer uId,
                            @RequestHeader(value = Constants.TENANT_ID, required = false,defaultValue = "") String tid) throws BizException {
        return ResponseFactory.ok(settingService.deleteByKey(key, tid));
    }

    @RequestMapping(value = "updateBatch", method = RequestMethod.POST)
    @ApiOperation(value = "批量更新配置", notes = "批量更新配置")
    //@BizLogger(description = "批量更新配置", serviceId = Constants.SERVICE_ID)
    public ResposeVO updateBatch(@RequestBody @Valid UpdateSettingBatchDTO updateSettingBatchDTO,
                                 @RequestHeader(TRANSPARENT_USERID_FEILD) Integer uId,
                                 @RequestHeader(value = Constants.TENANT_ID,required = false,defaultValue = "") String tid) throws BizException {
        if (settingService.updateSettingBatch(updateSettingBatchDTO, tid)) {
            settingService.keyMap(tid);
            return ResponseFactory.ok("操作成功");
        } else {
            return ResponseFactory.err("更新失败", EApiStatus.ERR_SYS);
        }
    }
}
